import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/routes/tab_home/controller/xindong_fujin_vm.dart';
import 'package:xindong/routes/tab_home/controller/xindong_tuijian_vm.dart';
import 'package:xindong/routes/tab_home/view/xindong_fujin_list_view.dart';
import 'package:xindong/routes/tab_home/view/xindong_tuijian_list_view.dart';

class XinDongHomeTabController extends GetxController {
  static XinDongHomeTabController get to => Get.find();

  late int currentIndex = 0;
  late TabController tabController;
  XinDongHomeNearbyVM? nearbyVM;
  XinDongHomeQualityVM? qualityVM;

  // Tab
  List<Widget> tabBar() {
    return [const Tab(text: "推荐"), const Tab(text: "附近")];
  }

  // TabView
  List<Widget> tabBarViews() {
    return [const XinDongHomeQualityListView(), const XinDongHomeNearbyListView()];
  }

  // 初始化
  void initWithVsync(TickerProvider vsync) {
    tabController = TabController(length: tabBar().length, vsync: vsync);
    tabController.addListener(() {
      updateListener();
    });
    qualityVM = XinDongHomeQualityVM();
    nearbyVM = XinDongHomeNearbyVM();
    Get.create(() => nearbyVM!);
    Get.create(() => qualityVM!);
  }

  void clear() {
    qualityVM?.dataList?.clear();
    nearbyVM?.dataList?.clear();
  }

  // TabController监听页面切换
  void updateListener() {
    currentIndex = tabController.index.round();

    if (currentIndex == 1) {
      // 定位
      // XinDongLocationCtr.to.startLocation();
    }
    update();
  }

  // ScrollNotification监听滚动位置切换
  void updateScrollNotification(double offset) {
    int index = (offset + screenWidth / 2).abs() ~/ screenWidth;
    currentIndex = index;
  }

  // 通知刷新
  void notifyRefresh() {
    if (currentIndex == 0) {
      qualityVM?.notifyRefresh();
    } else if (currentIndex == 1) {
      nearbyVM?.notifyRefresh();
    }
  }
}
